/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package data.Imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import data.Services.FriendListDAOService;
import model.entity.FriendList;
import model.entity.User;

/**
 *
 * @author ninkuhack
 */


public class FriendListServiceDAOImp implements FriendListDAOService{
    private static FriendListServiceDAOImp friendListDAO;
    
    public static FriendListServiceDAOImp getInstance() {
        if (friendListDAO == null) {
            friendListDAO = new FriendListServiceDAOImp();
        }
        return friendListDAO;
    }
    
    private FriendList createNewFriendList(ResultSet rs) throws SQLException{
        FriendList fl = new FriendList();
        fl.setFriendID(rs.getInt("friend_id"));
        fl.setStatus(rs.getInt("status"));
        fl.setUser(UserServiceDAOImp.getInstance().getUserByID(rs.getInt("user_id")));
        return fl;
    }
    
    @Override
    public FriendList getFriendListByID(int friendID, User userID) {
        FriendList object = null;
        Connection cn = null;
        ResultSet rs = null;
        try {
            String strSql = "select friend_id, user_id, status from friendlist where friend_id = ? and user_id=? and status=1";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            statement.setInt(1, friendID);
            statement.setInt(2, object.getUser().getUserID());
            rs = statement.executeQuery();
            while(rs.next()){
                object = createNewFriendList(rs);
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return object;
    }

    @Override
    public List<FriendList> getAllFriendLists() {
        Connection cn = null;
        List<FriendList> listFriend = new ArrayList<FriendList>();
        ResultSet rs = null;
        try {
            String strSql = "select friend_id, user_id, status from friendlist where status=1";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            rs = statement.executeQuery();
            while (rs.next()) {
                listFriend.add(createNewFriendList(rs));
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return listFriend;
    }

    @Override
    public boolean insertFriendList(FriendList friendList) {
        Connection cn = null;
        boolean flag = true;
        try {
            String strSql = "insert into friendlist(user_id, status) values(?,?)";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            statement.setInt(1, friendList.getUser().getUserID());
            statement.setInt(2, friendList.getStatus());
            int row = statement.executeUpdate();
            if(row <= 0){
                flag = false;
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return flag;
    }

    @Override
    public boolean updateFriendList(FriendList friendList) {
        Connection cn = null;
        boolean flag = false;
        try {
            String strSql = "update friendlist set status = ? where friend_id = ? and user_id = ?";
            PreparedStatement statement = cn.prepareStatement(strSql);
            cn = ConnectionManager.getConnection();
            statement.setInt(1, friendList.getStatus());
            statement.setInt(2, friendList.getFriendID());
            statement.setInt(3, friendList.getUser().getUserID());
            int row = statement.executeUpdate();
            if(row > 0){
                flag = true;
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return flag;
    }

    @Override
    public boolean deleteFriendList(FriendList friendList) {
        Connection cn = null;
        boolean flag = false;
        try {
            String strSql = "update friendlist set status=0 where friend_id = ? and user_id=?";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            statement.setInt(1, friendList.getFriendID());
            statement.setInt(2, friendList.getUser().getUserID());
            int row = statement.executeUpdate();
            if(row > 0){
                flag = true;
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return flag;
    }
    
    @Override
    public boolean deleteFriendList(int friendID) {
        Connection cn = null;
        boolean flag = false;
        try {
            String strSql = "update friendlist set status=0 where friend_id = ?";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            statement.setInt(1, friendID);
            int row = statement.executeUpdate();
            if(row > 0){
                flag = true;
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return flag;
    }

    @Override
    public boolean deleteFriendList(User user) {
        Connection cn = null;
        boolean flag = false;
        try {
            String strSql = "update friendlist set status=0 where user_id = ?";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            statement.setInt(1, user.getUserID());
            int row = statement.executeUpdate();
            if(row > 0){
                flag = true;
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return flag;
    }
    
    @Override
    public boolean deleteFriendListByUser(int userID) {
        Connection cn = null;
        boolean flag = false;
        try {
            String strSql = "update friendlist set status=0 where user_id=?";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            statement.setInt(1, userID);
            int row = statement.executeUpdate();
            if(row > 0){
                flag = true;
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return flag;
    }
    
    @Override
    public boolean restoreFriendList(int friendID) {
        Connection cn = null;
        boolean flag = false;
        try {
            String strSql = "update friendlist set status=1 where friend_id = ?";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            statement.setInt(1, friendID);
            int row = statement.executeUpdate();
            if(row > 0){
                flag = true;
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return flag;
    }

    @Override
    public List<FriendList> findFriendListsByUserID(int userID) {
        List<FriendList> list = new ArrayList<>();
        Connection cn = null;
        ResultSet rs = null;
        try {
            String strSql = "select friend_id, user_id, status from friendlist where user_id = ? and status = 1";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            statement.setInt(1, userID);
            rs = statement.executeQuery();
            while(rs.next()){
              list.add(createNewFriendList(rs));
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return list;
    }

    @Override
    public List<FriendList> findFriendListsByUserID(User user) {
        List<FriendList> list = new ArrayList<>();
        Connection cn = null;
        ResultSet rs = null;
        try {
            String strSql = "select friend_id, user_id, status from friendlist where user_id = ? and status = 1";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            statement.setInt(1, user.getUserID());
            rs = statement.executeQuery();
            while(rs.next()){
              list.add(createNewFriendList(rs));
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return list;
    }

    @Override
    public List<FriendList> findFriendListsByStatus(int status, User user) {
        List<FriendList> list = new ArrayList<FriendList>();
        Connection cn = null;
        ResultSet rs = null;
        try {
            String strSql = "select friend_id, user_id, status from friendlist where status = ? and user_id = ? and status = 1";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            statement.setInt(1, status);
            statement.setInt(2, user.getUserID());
            rs = statement.executeQuery();
            while(rs.next()){
                list.add(createNewFriendList(rs));
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return list;
    }

    @Override
    public ResultSet getFriendListByResultSet() {
        Connection cn = null;
        ResultSet rs = null;
        try {
            String strSql = "select friend_id, user_id, status from friendlist";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            rs = statement.executeQuery();
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return rs;
    }

    @Override
    public Vector<FriendList> getFriendListByVector() {
        Connection cn = null;
        Vector<FriendList> listFriend = new Vector<FriendList>();
        ResultSet rs = null;
        try {
            String strSql = "select friend_id, user_id, status from friendlist";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            rs = statement.executeQuery();
            while (rs.next()) {                
                FriendList friendList = new FriendList(rs.getInt("friend_id"), UserServiceDAOImp.getInstance().getUserByID(rs.getInt("user_id")), rs.getInt("status"));
                listFriend.add(friendList);
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return listFriend;
    }
}
